package com.nxj.dao.impl;

import com.nxj.annotation.Autowired;
import com.nxj.annotation.Repository;
import com.nxj.dao.TransferDao;
import com.nxj.pojo.Account;
import com.nxj.utils.ConnectionUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

@Repository
public class TransferDaoImpl implements TransferDao {

    @Autowired
    private ConnectionUtils connectionUtils;

    /*
    public void setConnectionUtils(ConnectionUtils connectionUtils) {
        this.connectionUtils = connectionUtils;
    }*/

    @Override
    public Account queryCardNo(String cardNo) throws Exception {
        Connection con = connectionUtils.getConnection();
        String sql = "select * from nxj_account where cardNo=?";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setString(1, cardNo);
        ResultSet resultSet = preparedStatement.executeQuery();

        Account account = new Account();
        while (resultSet.next()) {
            account.setCardNo(resultSet.getString("cardNo"));
            account.setMoney(resultSet.getInt("money"));
        }

        resultSet.close();
        preparedStatement.close();

        return account;
    }

    @Override
    public int updateCardNo(Account account) throws Exception {

        Connection con = connectionUtils.getConnection();
        String sql = "update nxj_account set money=? where cardNo=?";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setInt(1, account.getMoney());
        preparedStatement.setString(2, account.getCardNo());
        int i = preparedStatement.executeUpdate();

        preparedStatement.close();
        return i;
    }
}
